<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
</head>

<body>
    <div id="container">

    </div>

    <script>

        //创建一个观察者
        function observer(target) {
            const div = document.getElementById("container");

            const proxy = new Proxy(target, {
                set(target, prop, value) {
                    Reflect.set(target, prop, value);
                    render();
                },
                get(target, prop){
                    return Reflect.get(target, prop);
                }
            });

            render();

            function render() {
                let html = "";
                for (const prop of Object.keys(target)) {
                    html += `
                        <p><span>${prop}：</span><span>${target[prop]}</span></p>
                    `;
                }
                div.innerHTML = html;
            }

            return proxy;
        }

        const target = {
            a: 1,
            b: 2
        }
        const obj = observer(target);  

    </script>
</body>

</html>